{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Question\n",
    "Given an array, check whether the array is in sorted order with recursion.(Narasimha)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-05T15:43:24.507965Z",
     "start_time": "2019-09-05T15:43:24.504046Z"
    }
   },
   "outputs": [],
   "source": [
    "from typing import List, Union\n",
    "Num = Union[int, float]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-05T15:43:25.676109Z",
     "start_time": "2019-09-05T15:43:25.667322Z"
    }
   },
   "outputs": [],
   "source": [
    "def increase(xs: List[Num], n: int) -> bool:\n",
    "    if n < 0:\n",
    "        raise \"The length of list must >= 0\"\n",
    "    elif n <= 1:\n",
    "        return True\n",
    "    else:\n",
    "        return (xs[n-1] >= xs[n-2]) and increase(xs, n-1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-05T15:44:24.138423Z",
     "start_time": "2019-09-05T15:44:24.125584Z"
    }
   },
   "outputs": [],
   "source": [
    "def decrease(xs: List[Num], n: int) -> bool:\n",
    "    if n < 0:\n",
    "        raise \"The length of list must >= 0\"\n",
    "    elif n <= 1:\n",
    "        return True\n",
    "    else:\n",
    "        return (xs[n-1] <= xs[n-2]) and decrease(xs, n-1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-05T15:45:11.599833Z",
     "start_time": "2019-09-05T15:45:11.592568Z"
    }
   },
   "outputs": [],
   "source": [
    "def monotone(xs: List[Num]) -> bool:\n",
    "    n = len(xs)\n",
    "    return increase(xs, n) or decrease(xs, n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-05T15:45:24.997985Z",
     "start_time": "2019-09-05T15:45:24.981425Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# test1\n",
    "monotone([1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-05T15:45:34.761684Z",
     "start_time": "2019-09-05T15:45:34.742387Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# test2\n",
    "monotone([1, 3, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-05T15:45:53.589771Z",
     "start_time": "2019-09-05T15:45:53.581847Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# test3\n",
    "monotone([3, 2, -1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
